/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SubscriptionBDImpl.java
 * 
 * Created on Jan 4, 2009 at 10:06:21 PM
 */

package org.robcash.apps.messagepopup.dataaccess.business.impl;

import static org.robcash.apps.messagepopup.BeanNameConstants.SUBSCRIPTION_STORE_BEAN_NAME;

import org.robcash.apps.messagepopup.MessageException;
import org.robcash.apps.messagepopup.dataaccess.business.SubscriptionBD;
import org.robcash.apps.messagepopup.dataaccess.dao.SubscriptionDAO;
import org.robcash.apps.messagepopup.dataaccess.util.Transformer;
import org.robcash.apps.messagepopup.vo.SubscriptionStore;
import org.robcash.xmlns.apps._2009._01.MessagePopup.SubscriptionStoreType;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * Business delegate for the subscription store service
 * @author Rob
 */
public class SubscriptionBDImpl implements SubscriptionBD, BeanFactoryAware {

    /**
     * Bean factory
     */
    private BeanFactory beanFactory;
    
    /**
     * DAO
     */
    private SubscriptionDAO dao;

    /**
     * Transformer
     */
    private Transformer<SubscriptionStoreType, SubscriptionStore> transformer;

    /**
     * Set the transformer
     * @param transformer Subscription service transformer
     */
    public void setTransformer(Transformer<SubscriptionStoreType, SubscriptionStore> transformer) {
        this.transformer = transformer;
    }

    /**
     * Set the DAO
     * @param dao Subscription data access object
     */
    public void setSubscriptionDAO(SubscriptionDAO dao) {
        this.dao = dao;
    }

    /**
     * Set the Spring bean factory
     * @param factory Spring bean factory that created this instance
     */
    public void setBeanFactory(BeanFactory factory) {
        this.beanFactory = factory;
    }

    /**
     * Load a subscription store
     * @return Subscriptions store
     * @throws MessageException Thrown if the subscription store cannot be loaded
     */
    public SubscriptionStore loadSubscriptionStore()
            throws MessageException {
        // Load store from its location
        SubscriptionStoreType storeInput = dao.loadSubscriptionStore();

        SubscriptionStore store = null;
        if ( storeInput != null ) {
            // Subscription store was found - transform it
            store = transformer.toValueObject(storeInput);
        }
        else {
            // No subscription store was found - create one
            store = (SubscriptionStore) beanFactory.getBean(
                    SUBSCRIPTION_STORE_BEAN_NAME);
        }

        return store;
    }

    /**
     * Save a subscription store
     * @param subscriptionStore Subscription store to save
     * @throws MessageException Thrown if the subscription store cannot be saved
     */
    public void saveSubscriptionStore(SubscriptionStore subscriptionStore)
            throws MessageException {

        // Transform and save the subscription store
        dao.saveSubscriptionStore(transformer.toDataObject(subscriptionStore));
    }

}
